import 'package:flutter/material.dart';
import 'package:like_chat/blocs/friends_bloc.dart';
import 'package:like_chat/entity/user.dart';
import 'package:like_chat/final_values.dart';
import 'package:like_chat/widget/header_widget.dart';
import 'package:like_chat/widget/icon_widget.dart';
import 'package:like_chat/widget/index_listview_widget.dart';


class FriendListPage extends StatefulWidget {
  final FriendsBloc bloc;
  final User user;

  const FriendListPage({this.user, this.bloc});

  @override
  _FriendListPageState createState() => _FriendListPageState();
}

class _FriendListPageState extends State<FriendListPage>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  List<String> indexListData;
  List<FriendIndexModel> friendIndexModels0;
  List<FriendIndexModel> friendIndexModels;

  @override
  void initState() {
    super.initState();
    indexListData = [
      "A",
      "B",
      "C",
      "D",
      "E",
      "F",
      "G",
      "H",
      "I",
      "J",
      "K",
      "L",
      "M",
      "N",
      "O",
      "P",
      "Q",
      "R",
      "S",
      "T",
      "U",
      "V",
      "W",
      "X",
      "Y",
      "Z"
    ];
    friendIndexModels0 = indexListData.expand((i) {
      List<FriendIndexModel> f = [];
      f.add(FriendIndexModel(index: i));
      for (var j = 0; j < 5; j++) {
        f.add(FriendIndexModel(friendName: "$i-$j", url: IMG_URL));
      }
      return f;
    }).toList();
    friendIndexModels = [
      FriendIndexModel(url: "-1", friendName: "新的朋友"),
      FriendIndexModel(url: "-2", friendName: "群聊"),
      FriendIndexModel(url: "-3", friendName: "标签"),
      FriendIndexModel(url: "-4", friendName: "公众号"),
    ];
    friendIndexModels.addAll(friendIndexModels0);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexListView(
        modelList: friendIndexModels,
        indexList: indexListData,
        modelHandler: (data) {
          var fData = (data as FriendIndexModel);
          return Column(
            children: <Widget>[
              Container(
                padding: const EdgeInsets.only(
                    left: 10.0, right: 10.0, top: 5.0, bottom: 5.0),
                alignment: Alignment.centerLeft,
                child: Row(
                  children: <Widget>[
                    selectorIcon(fData.url),
                    const SizedBox(
                      width: 10.0,
                    ),
                    Text(
                      fData.friendName,
                      style: TextStyle(fontSize: 16.0),
                    )
                  ],
                ),
                height: 59.0,
              ),
              const Padding(
                padding: EdgeInsets.only(left: 10.0, right: 20.0),
                child: Divider(
                  height: 1.0,
                ),
              ),
            ],
          );
        },
        indexHandler: (index) {
          return Container(
            padding: const EdgeInsets.symmetric(horizontal: 10.0),
            alignment: Alignment.centerLeft,
            child: Text(index),
            color: const Color(backgroundColorValue),
            height: 30.0,
          );
        },
        showIndexHandler: (index) {
          return Center(
            child: Container(
              alignment: Alignment.center,
              width: 80.0,
              height: 80.0,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(5.0),
                color: Colors.grey[500],
              ),
              child: Text(
                index,
                softWrap: false,
                style: TextStyle(
                  fontSize: 40.0,
                  color: Colors.white,
                ),
              ),
            ),
          );
        },
        modelWidgetHeight: 60.0,
        indexWidgetHeight: 30.0,
      ),
    );
  }

  Widget selectorIcon(String id) {

    switch (id) {
      case "-1" : return IconWidget(width: 40.0, heigth: 40.0, 
      icon: Icon(Icons.person_add, color: Colors.white,), color: const Color(0xfffa9d3b),);

      case "-2" : return IconWidget(width: 40.0, heigth: 40.0, 
      icon: Icon(Icons.supervised_user_circle, color: Colors.white,), color: const Color(0xff45c01a),); 

      case "-3" : return IconWidget(width: 40.0, heigth: 40.0, 
      icon: Icon(Icons.local_offer, color: Colors.white,), color: const Color(0xff2782d7),); 

      case "-4" : return IconWidget(width: 40.0, heigth: 40.0, 
      icon: Icon(Icons.person, color: Colors.white,), color: const Color(0xff2782d7),);
      
      default: return HeaderWidget(radius: 40.0, url: id, unreadNum: 0,);
    }
  }
}
